package br.edu.ufcg.sri;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import br.edu.ufcg.sri.armazenamento.gui.Sinonimizador;
import br.edu.ufcg.sri.indexacao.GUI;
import br.edu.ufcg.sri.recuperacao.gui.MainGUI;

/**
 * ProfileChecker main class.
 * 
 * @author Matheus
 */
public class Main {

	
	private static final String ARMAZENAMENTO_GUI_OPTION = "armazenamentoGUI";
	
	private static final String INDEXADOR_GU_OPTION = "indexadorGUI";

	
	private static final String SEARCH_GUI_OPTION = "pesquisaGUI";

	/** Help option. */
	private static final String HELP_OPTION = "help";

	public static void main(String[] args) {
		Option armazenamentoGUIOption = new Option(ARMAZENAMENTO_GUI_OPTION,
				"Inicializa a GUI de armazenamento");
		Option searchGUIOption = new Option(SEARCH_GUI_OPTION, 
				"Inicializa a GUI de pesquisa");
		Option indexadorGUIOption = new Option(INDEXADOR_GU_OPTION, 
				"Inicializa a GUI do indexador");
		Option help = new Option(HELP_OPTION, "This help message");

		Options options = new Options();

		options.addOption(armazenamentoGUIOption);
		options.addOption(searchGUIOption);
		options.addOption(indexadorGUIOption);
		options.addOption(help);

		CommandLineParser parser = new GnuParser();
		
		try {
			CommandLine line = parser.parse(options, args);
			if (line.hasOption(SEARCH_GUI_OPTION)) {
				MainGUI.initMainGUI();
			} else if (line.hasOption(ARMAZENAMENTO_GUI_OPTION)) {
		        java.awt.EventQueue.invokeLater(new Runnable() {
		            public void run() {
		                new Sinonimizador().setVisible(true);
		            }
		        });
			} else if (line.hasOption(INDEXADOR_GU_OPTION)) {
				GUI gui = new GUI();
				gui.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent e) {
						System.exit(0);
					}
				});
			} else {
				// automatically generate the help statement
				HelpFormatter formatter = new HelpFormatter();
				formatter.printHelp("java -jar sriIndexer.jar", options);
			}
		} catch (ParseException exp) {
			System.err.println("Parsing failed.  Reason: " + exp.getMessage());
		}

	}

}